#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;

struct State {
    int last_word_idx;
    int current_len;
    vector<int> usage;
};

int main() {
    int n;
    cin >> n;
    vector<string> words(n);
    for (int i = 0; i < n; ++i) {
        cin >> words[i];
    }
    char start_char;
    cin >> start_char;

    queue<State> q;
    int max_length = 0;
    vector<int> initial_usage(n, 0);

    for (int i = 0; i < n; ++i) {
        if (words[i][0] == start_char) {
            vector<int> usage = initial_usage;
            usage[i] = 1;
            q.push({i, static_cast<int>(words[i].size()), usage});
        }
    }

    while (!q.empty()) {
        State current = q.front();
        q.pop();
        if (current.current_len > max_length) {
            max_length = current.current_len;
        }
        const string& last_word = words[current.last_word_idx];
        for (int i = 0; i < n; ++i) {
            if (current.usage[i] >= 2) {
                continue;
            }
            const string& next_word = words[i];
            int max_possible = min(last_word.size(), next_word.size());
            int overlap = 0;
            for (int o = 1; o <= max_possible; ++o) {
                if (last_word.substr(last_word.size() - o) == next_word.substr(0, o)) {
                    overlap = o;
                }
            }
            if (overlap == 0) {
                continue;
            }
            if (overlap == next_word.size() || overlap == last_word.size()) {
                continue;
            }
            vector<int> new_usage = current.usage;
            new_usage[i]++;
            int new_len = current.current_len + next_word.size() - overlap;
            q.push({i, new_len, new_usage});
        }
    }

    cout << max_length << endl;
    return 0;
}
